<template>
  <div>
    <el-form :model="form" ref="form" label-width="120px" :rules="rules">
      <el-form-item label="角色名" prop="roleName">
        <el-input type="text" v-model="form.roleName"/>
      </el-form-item>
      <el-form-item>
        <el-button type="success" size="small" @click="submit">提交</el-button>
        <el-button type="danger" size="small" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { get, post } from '@/plugins/axios'
import { Message } from "element-ui"

export default {
  name: "edit",
  props: ['id'],
  created() {
    if (this.id != null) {
      this.getOne()
    }
  },
  data() {
    const module = '/role'
    return {
      uri: {
        getOne: module + '/selectOne/',
        saveOrUpdate: module + '/saveOrUpdate'
      },
      form: {
        roleName: '',
      },
      rules: {
        roleName: [
          {required: true, message: '请输入角色名', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          post(this.uri.saveOrUpdate, this.form, response => {
            if (response.code === 200) {
              Message.success(response.message)
            }
            this.$emit('update:editDialogShow',false)
            this.$emit('getRoleData')
          })
        } else {
          return false
        }
      });
    },
    cancel() {
      this.$emit('update:editDialogShow',false)
    },
    getOne() {
      get(this.uri.getOne + this.id, {}, response => {
        for (const key in this.form) {
          this.form[key] = response.data[key]
        }
        this.form.id = response.data.id
      })
    }
  }
}
</script>

<style scoped lang="less">

</style>